package com.movecar.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;

import com.google.gson.Gson;
import com.movecar.R;
import com.movecar.expandableListView.TreeViewAdapter;
import com.movecar.model.GroupClassificationBcListBean;
import com.movecar.model.GroupClassificationBean;
import com.movecar.model.GroupClassificationListBean;
import com.movecar.utils.Alert;
import com.movecar.utils.AppConfig;
import com.movecar.utils.DataStore;
import com.movecar.utils.HttpPostRequestAsyncTask;
import com.movecar.utils.RequestResultCallback;
import com.movecar.widget.CustomProgressDialog;

/**
 * 活动分类
 * 
 * @author Zhao.JQ
 * 
 */
public class GroupClassificationActivity extends Activity implements OnClickListener, OnItemClickListener {

	private ExpandableListView expandablelistview;
	private CustomProgressDialog progressDialog = null;
	private GroupClassificationBean mGroupClassificationBean;
	public String[] groups_dalian = { "全部", "西岗区", "中山区", "沙河口区", "甘井子区", "金州新区", "近郊" };
	public String[][] childs_dalian = { { "全部" }, { "奥林匹克广场", "人民广场/新开路", "大菜市/站北广场", "五一广场", "八一路" },
			{ "青泥洼桥", "天津街/人民路", "二七广场", "中山广场/延安路", "桃源", "港湾广场", "老虎滩", "三八广场", "友好广场" },
			{ "西安路沿线", "星海湾", "黑石礁", "和平广场", "马栏子", "星海公园", "南沙" },
			{ "周水子", "高新园区", "泉水", "华南广场", "老甘井子", "老甘井子", "山东路", }, { "金州区", "双D港", "开发区", "保税区", "金石滩旅游度假区" },
			{ "旅顺口区", "瓦房店市", "庄河市", "普兰店市", "长海县" } };
	public String[] groups_wuxi = { "全部", "崇安区", "滨湖区", "南长区", "北塘区", "新区", "锡山区", "惠山区", "江阴市", "宜兴市" };
	public String[][] childs_wuxi = {
			{ "全部" },
			{ "1912酒吧街", "保利广场", "崇安寺", "大成巷/东方广场", "哥伦布广场", "广瑞街道", "汇金广场/八佰伴", "火车站/恒隆广场", "宁海里/尤渡苑", "苏宁广场", "上马墩",
					"天棚食品城", "远东百货/胜利门", "中山路", "中华美食城" },
			{ "春潮花园", "胡埭", "海岸城", "河埒口", "华莱坞", "湖滨商业街", "湖滨路", "江南大学城", "梁清路", "蠡园/太湖景区", "落霞苑/华庄", "梁溪路沿线", "灵山景区",
					"梅园", "万象城/金城湾", "滨湖万达广场", "万科酩悦/东绛", "新安", "新体育中心/奥林花园" },
			{ "东林书院", "茂业天地", "南长街/阳春巷", "南下塘", "南禅寺", "清扬路沿线", "新天地广场", "西水东广场", "阳光购物广场", "扬名花园", "中桥/芦庄", "招商城" },
			{ "康桥丽景", "广石路沿线", "惠山古镇", "金太湖广场", "刘潭", "民丰里", "青石路/北大街", "盛岸路沿线", "五爱广场/五爱路", "运河公园" },
			{ "宝龙城市广场", "长江北路沿线", "坊前", "新区哥伦布广场", "梅村", "硕放", "旺庄", "新地假日广场", "新之城广场" },
			{ "安镇", "八士", "查桥", "东亭/东北塘", "荡口古镇", "丰汇欢乐广场", "港下", "宜家/荟聚云林", "羊尖" },
			{ "长安", "惠山万达广场", "洛社", "藕塘", "钱桥", "前洲", "玉祁", "时代广场", "西漳", "杨市", "阳山", "堰桥" },
			{ "华士镇", "璜土镇", "南闸", "山观镇", "申港街道", "夏港街道", "新桥镇", "云亭街道", "月城镇", "祝塘镇", "周庄镇" },
			{ "步行街/西横街", "城北大排档", "丁蜀", "大觉寺/横山水库", "官林", "环科园/城铁站", "和桥/屺亭", "家和花园/大润发", "解放路", "金三角汽车站", "人民医院/文化广场",
					"人民南路/森林公园", "善卷洞", "万达及周边", "现代生活广场/大润发", "徐舍", "新天地/太滆桥", "宜兴电大", "张渚", "竹海/陶祖圣境" } };

	List<GroupClassificationListBean> groups = new ArrayList<GroupClassificationListBean>();
	List<List<GroupClassificationBcListBean>> childs = new ArrayList<List<GroupClassificationBcListBean>>();
	private TreeViewAdapter adapter;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.group_classification_gridview);
		startProgressDialog();
		getGroupClassification();

	}

	private void initView() {

		adapter = new TreeViewAdapter(this, 38);
		expandablelistview = (ExpandableListView) findViewById(R.id.expandablelistview);
		Button btn_left = (Button) findViewById(R.id.btn_left);

		TextView title = (TextView) findViewById(R.id.title);
		title.setText("分类");
		btn_left.setVisibility(View.VISIBLE);
		btn_left.setOnClickListener(this);

		List<TreeViewAdapter.TreeNode> treeNode = adapter.getTreeNode();
		for (int i = 0; i < groups.size(); i++) {
			TreeViewAdapter.TreeNode node = new TreeViewAdapter.TreeNode();
			node.parent = groups.get(i).getArea();
			for (int j = 0; j < childs.get(i).size(); j++) {
				node.childs.add(childs.get(i).get(j).getBc());
			}
			treeNode.add(node);
		}
		adapter.updateTreeNode(treeNode);
		expandablelistview.setAdapter(adapter);
		expandablelistview.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
				Intent intent = new Intent();
				intent.putExtra("childPosition", childs.get(groupPosition).get(childPosition).getBc());
				setResult(RESULT_OK, intent);
				finish();

				return false;
			}
		});

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btn_left:
			finish();
			break;

		default:
			break;
		}

	}

	private void startProgressDialog() {
		if (progressDialog == null) {
			progressDialog = CustomProgressDialog.createDialog(this);
			progressDialog.setMessage("正在加载...");
		}

		progressDialog.show();
	}

	private void stopProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

	/**
	 * 取得商圈
	 * 
	 */
	@SuppressWarnings("unchecked")
	private void getGroupClassification() {

		List<NameValuePair> params = new ArrayList<NameValuePair>(); // 使用NameValuePair来保存要传递的Post参数
		params.add(new BasicNameValuePair("cmd", "queryBcNew1")); // 添加要传递的参数
		params.add(new BasicNameValuePair("userId", DataStore.getInstance().getUserId()));
		new HttpPostRequestAsyncTask(new RequestResultCallback() {

			@Override
			public void onSuccess(Object object) {
				stopProgressDialog();
				String result = (String) object;
				Gson gson = new Gson();
				try {
					mGroupClassificationBean = gson.fromJson(result, GroupClassificationBean.class);
				} catch (Exception e) {
					return;
				}
				groups = mGroupClassificationBean.getList();
				for (int i = 0; i < groups.size(); i++) {

					childs.add(groups.get(i).getBcList());
				}
				initView();
			}

			@Override
			public void onFail(String exception) {
				stopProgressDialog();
				Alert.error(GroupClassificationActivity.this, "与服务器连接失败，请检查网络");

			}
		}, GroupClassificationBean.class, AppConfig.USER).execute(params);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
		if (requestCode == 0) {
			if (resultCode == RESULT_OK) {
				finish();
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

	}

}
